package com.example.music_player
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.provider.MediaStore
import android.util.Log
import androidx.core.app.NotificationCompat

class MusicService : Service() {
    companion object {
        val Commond = "Operate"
    }
    val mediaPlayer = MediaPlayer()
    val musicList = mutableListOf<String>()
    val musicNameList = mutableListOf<String>()
    var current = 0
    var isPause = false
    val binder = MusicBinder()

    inner class MusicBinder : Binder() {
        val musicName
            get() = musicNameList.get(current)
        var currentPosition
            get() = mediaPlayer.currentPosition
            set(value) = mediaPlayer.seekTo(value)
        val duration
            get() = mediaPlayer.duration
        val size
            get() = musicList.size
        val currentIndex
            get() = current
    }
    override fun onCreate() {
        super.onCreate()
        getMusicList()
        mediaPlayer.setOnPreparedListener {
            it.start()
        }
        mediaPlayer.setOnCompletionListener {
            current++
            if (current >= musicList.size){
                current = 0
            }
            play()
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val operate  = intent?.getIntExtra(Commond,1) ?: 1
        when(operate){
            1 -> play()
            2 -> pause()
            3 -> stop()
            4 -> next()
            5 -> prev()
        }
        return super.onStartCommand(intent, flags, startId)
    }
    fun pause(){
        if (isPause){
            mediaPlayer.start()
            isPause = false
        }else{
            mediaPlayer.pause()
            isPause = true
        }
    }
    fun stop(){
        mediaPlayer.stop()
        stopSelf()
    }
    fun next(){
        current++
        if (current >= musicList.size){
            current = 0
        }
        play()
    }
    fun prev(){
        current--
        if (current < 0){
            current = musicList.size - 1
        }
        play()
    }
    fun play(){
        if(musicList.size == 0) return
        val path = musicList[current]
        mediaPlayer.reset()
        try {
            mediaPlayer.setDataSource(path)
            mediaPlayer.prepareAsync()
        }catch (e: Exception){
            e.printStackTrace()
        }
    }
    override fun onBind(intent: Intent): IBinder {
        return binder
    }
    private fun getMusicList() {
        val cursor = contentResolver.query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null,
            null
        )
        cursor?.use {
            val titleIndex = it.getColumnIndex(MediaStore.Audio.Media.TITLE)
            val pathIndex = it.getColumnIndex(MediaStore.Audio.Media.DATA)
            while (it.moveToNext()) {
                if (titleIndex != -1 && pathIndex != -1) {
                    val title = it.getString(titleIndex)
                    musicNameList.add(title)
                    val path = it.getString(pathIndex)
                    musicList.add(path)
                }
            }
        } ?: Log.e("MusicService", "Cursor is null")
    }


}